package com.banshou.installs.ui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;

import com.banshou.installs.R;
import com.banshou.installs.constant.Config;
import com.banshou.installs.databinding.NearCommBinding;
import com.banshou.installs.model.dao.RequestDy;
import com.banshou.installs.model.dao.ResponseDY;
import com.banshou.installs.model.net.Observer;
import com.banshou.installs.model.net.ResponseInfo;
import com.banshou.installs.model.net.RetrofitManager2;
import com.banshou.installs.model.net.RxUtil;
import com.banshou.installs.ui.adapter.DYAdapter;
import com.banshou.installs.utils.DialogUtils;
import com.banshou.installs.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cjq on 2018/4/19.
 * Email: stoic_yb@139.com
 * features:
 */
public class DYActivity extends BaseActivity {
    private NearCommBinding mBinding;
    private DYAdapter mAdapter;
    private List<ResponseDY.DYList> dyList;
    private DialogUtils dialogUtils;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.near_comm);
        initView();
    }

    private void initView() {
        dyList = new ArrayList<>();
        dialogUtils = new DialogUtils();
        mBinding.title.pagerTitle.setText("选择单元");
        mBinding.title.back.setOnClickListener(view -> finish());
        mAdapter = new DYAdapter(this, R.layout.nearcomm_item, dyList);
        mBinding.recycleview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mBinding.recycleview.setAdapter(mAdapter);
        getList();
        getId();
    }

    private void getList() {
        String zhuangId = getIntent().getStringExtra("ZID");
        if (zhuangId != null) {
            dialogUtils.showAni(this);
            RetrofitManager2.getInstance()
                    .create(ResponseInfo.class)
                    .getDY(zhuangId)
                    .compose(RxUtil.lifecycle(this))
                    .subscribe(new Observer<ResponseDY>() {
                        @Override
                        protected void onNextStep(ResponseDY responseDY) {
                            dialogUtils.hideAni();
                            dyList.clear();
                            dyList.addAll(responseDY.getBuildings());
                            mAdapter.notifyDataSetChanged();
                        }

                        @Override
                        protected void onErrorResult() {
                            dialogUtils.hideAni();
                        }
                    });

        }
    }

    private void getId() {
        mAdapter.setOnItemOnClickListener((Id, NAME) -> {
            Intent intent = new Intent();
            intent.putExtra("DYID", Id);
            intent.putExtra("DYNAME", NAME);
            setResult(3, intent);
            finish();
        });
    }
}
